// Variable app, Cordova init
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

//Google API
var GOOGLE = {
    map : {},
    setConfig : function(){
        google.maps.visualRefresh = true;
    },
    getLatAndLongFromAddress : function(address, callback){
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                callback(latitude, longitude);
                console.log(latitude + ' ' + longitude);
            }
        });
    },
    initialize : function(lat, lng) {
      var mapOptions = {
        zoom: 12,
        center: new google.maps.LatLng(lat, lng)
      };
      this.map = new google.maps.Map(document.getElementById('map'), mapOptions);
    },
    addMarker : function(lat, lng, title, color, icon){
        var myLatlng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: this.map,
            animation: google.maps.Animation.DROP,
            title: title,
            cursor: 'pointer',
            icon : 'img/'+ color + '/' + icon + '.png'
        });
    },
    addGeolocMarker : function(){
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                this.map.setCenter(pos);
                this.addMarker(position.coords.latitude, position.coords.longitude, 'blue', 'void');
            }, function (){
                alert('ok');
            });
          } else {
            alert("Your browser doesn't support geolocation, please check your parameters.");
        }
    }
}

//Gestion de l'historique (site web monopage)
var HISTORY = {
    tab : ["index"],
    show : function(id){
        $(".page").hide();
        $("#" + id).show();
    },
    navigate : function(id){
        this.tab.push(id);
        this.show(id);
    },
    back : function(){        
        if(this.tab.length > 1){
            this.tab.pop();
            var id = this.tab[this.tab.length - 1];
            this.show(id);
        }
    }
}

//Global init
var INIT = {
    postLoad : function(){
        $("#checkevent").show();
    }
}

//DOM ready function
$(function() {
      INIT.postLoad();
      GOOGLE.setConfig();
      GOOGLE.initialize(48, 2);
      GOOGLE.addGeolocMarker();
});